﻿using Approval.API.Applcation.Command.AcceptanceRelationCommand;
using Approval.Domain;
using Approval.Domain.Common;
using Approval.Infrastructure.Impl;
using Approval.Infrastructure.Interface;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Approval.API.Applcation.CommandHandler.AcceptanceRelationCommand
{
    public class AcceptanceRelationApprovalCommandHandler : IRequestHandler<AcceptanceRelationApprovalCommand, Audit>
    {
        private readonly IAcceptanceRelationRepository acceptanceRelationRepository;
        private readonly Cross_services_Helper cross_Services_Helper;

        public AcceptanceRelationApprovalCommandHandler(IAcceptanceRelationRepository acceptanceRelationRepository, Cross_services_Helper cross_Services_Helper)
        {
            this.acceptanceRelationRepository = acceptanceRelationRepository;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<Audit> Handle(AcceptanceRelationApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var info = acceptanceRelationRepository.All().Result.Where(x => x.AId == request.AId && x.ApprovalId == request.UserId).FirstOrDefault();
                //审批时间
                info.CreateDate = DateTime.Now;
                //审批备注
                info.ApprovalDesc = request.ApprovalDesc;
                //同意
                if (request.Identifier == 1)
                {
                    info.ApprovalState = 2;
                    await acceptanceRelationRepository.Update(info);
                    var hctapprocal = new AcceptanceRelation()
                    {
                        AId = request.AId,
                        ApprovalId = request.TransferId,
                        ApprovalState = 1,
                        ApprovalName = request.TransferName,
                        ApprovalDesc = null
                    };
                    var res = await acceptanceRelationRepository.Create(hctapprocal);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.TransferName,
                        CreateById = request.TransferId,
                        AuditState = 1,
                        IsDel = false
                    };
                    return audit;
                }
                //拒绝
                if (request.Identifier == 2)
                {
                    info.ApprovalState = 3;
                    await acceptanceRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改领用状态
                    var Accepance = await cross_Services_Helper.Get($"http://10.31.60.9:8000/kdrout/api/RequisitionRApi/GetPurchaseInfo?id={request.AId}");
                    var Accepances = JsonConvert.DeserializeObject<AccepanceDto>(Accepance);
                    var AccepanceList = new AccepanceState();
                    AccepanceList.id = Accepances.Id;
                    AccepanceList.auditState = 3;
                    Accepances.ApprovedBy = "小明";
                    var res = await cross_Services_Helper.Post($"http://10.31.60.9:8000/kkrout/api/RequisitionAPI/RequisitionUpdateStateShenPi", AccepanceList);

                    return audit;
                }
                //通过并终止转批
                if (request.Identifier == 3)
                {
                    info.ApprovalState = 2;
                    await acceptanceRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改领用状态
                    var Accepance = await cross_Services_Helper.Get($"http://10.31.60.9:8000/kdrout/api/RequisitionRApi/GetPurchaseInfo?id={request.AId}");
                    var Accepances = JsonConvert.DeserializeObject<AccepanceDto>(Accepance);
                    var AccepanceList = new AccepanceState();
                    AccepanceList.id = Accepances.Id;
                    AccepanceList.auditState = 3;
                    Accepances.ApprovedBy = "小明";
                    var res = await cross_Services_Helper.Post($"http://10.31.60.9:8000/kkrout/api/RequisitionAPI/RequisitionUpdateStateShenPi", AccepanceList);


                    return audit;
                }


                return info;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
            
        }
    }
}
